<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link href="https://cdn.bootcss.com/element-ui/2.13.1/theme-chalk/index.css" rel="stylesheet">

    <!--<link rel="stylesheet" href="../plugins/elementui/index.css">-->
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统管理
            <small>菜单管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table
                    :data="dataList"
                    style="width: 100%"
                    row-key="id"
                    border
                    lazy
                    :load="load"
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column
                        prop="name"
                        label="菜单名称"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="linkUrl"
                        label="访问路径"
                        width="180">
                </el-table-column>

                <el-table-column
                        prop="path"
                        label="路径级别">
                </el-table-column>
                <el-table-column
                        prop="parentMenuId"
                        label="自关联Id">
                </el-table-column>
                <el-table-column
                        prop="level"
                        label="菜单级别">
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page.sync="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单项" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="路径级别" prop="path">
                                    <el-input v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级" prop="priority">
                                    <el-input v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <!--<el-form-item label="自关联Id" prop="parentMenuId">-->

                                    <!--<el-input v-model="formData.parentMenuId"/>-->
                                <!--</el-form-item>-->
                                <el-form-item label="自关联Id" prop ="parentMenuId">
                                    <el-select v-model="formData.parentMenuId" placeholder="请选择关联的菜单">
                                        <el-option v-for = " menu in menuList"
                                                :label="menu.name"
                                                :value="menu.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="菜单级别" prop="level">
                                    <el-input v-model="formData.level"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="图标">
                                    <el-input v-model="formData.icon"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="菜单说明">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑菜单项" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="路径级别" prop="path">
                                    <el-input v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级" prop="priority">
                                    <el-input v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <!--<el-form-item label="自关联Id" prop="parentMenuId">-->

                                <!--<el-input v-model="formData.parentMenuId"/>-->
                                <!--</el-form-item>-->
                                <el-form-item label="自关联Id">
                                    <el-select v-model="formData.parentMenuId" placeholder="请选择关联的菜单">
                                        <el-option v-for = " menu in menuList"
                                                   :label="menu.name"
                                                   :value="menu.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="菜单级别" prop="level">
                                    <el-input v-model="formData.level"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="图标">
                                    <el-input v-model="formData.icon"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="菜单说明">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="https://cdn.bootcss.com/element-ui/2.13.1/index.js"></script>
<!--<script src="../plugins/elementui/index.js"></script>-->
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script src="../js/authority.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            menuList:{},
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false, //编辑表单是否可见
            value:"100",
            rules: {//校验规则
                name: [{required: true, message: '菜单名称为必填项', trigger: 'blur'}],
                linkUrl: [{required: true, message: '访问路径为必填项', trigger: 'blur'}],
                path: [{required: true, message: '访问路径为必填项', trigger: 'blur'}],
                priority: [{required: true, message: '访问路径为必填项', trigger: 'blur'}],
                // parentMenuId: [{required: true, message: '访问路径为必填项', trigger: 'blur'}],
                level: [{required: true, message: '访问路径为必填项', trigger: 'blur'}],
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            load(tree, treeNode, resolve) {
                console.log(tree);
                console.log(treeNode);
                setTimeout(() => {
                    resolve()
                }, 1000)
            },
            //编辑
            handleEdit() {
                this.$refs["dataEditForm"].validate((valid) => {
                    if (valid){
                        console.log(this.formData);
                        axios.post("/menu/edit.do",this.formData).then((res)=>{
                            //编辑成功
                            if (res.data.flag){
                                this.dialogFormVisible4Edit = false;
                                this.$message.success(res.data.message);
                                this.findPage();
                            }
                            //编辑失败 即存在重复编码或者信息
                            else {
                                // this.$message.error(res.data.message + "【"+res.data.data+"】")
                                this.$message.error(res.data.message)
                            }
                        })
                    } else {
                        this.$message.error("表单数据校验失败，请检查你的输入")
                    }
                })

            },
            //添加
            handleAdd() {
                this.$refs['dataAddForm'].validate((valid) => {
                    //表单校验通过
                    if (valid) {
                        console.log(this.formData);
                        axios.post("/menu/add.do", this.formData).then((res) => {
                            //新增成功
                            if (res.data.flag) {
                                this.dialogFormVisible = false;
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                                this.findPage();
                            }
                            //新增失败
                            else {
                                console.log(res.data);
                                this.$message.error(res.data.message + '【' + res.data.data + '】')
                            }
                        })
                    }
                    //表单校验失败
                    else {
                        this.$message.error('表单数据校验失败，请检查输入')
                    }
                })
            },
            //分页查询
            findPage() {
                axios.post("/menu/findPage.do", this.pagination).then((res) => {
                    //查询成功
                    if (res.data.flag) {
                        // this.$message({
                        //     message: res.data.message,
                        //     type: 'success'
                        // });
                        this.dataList = res.data.data.rows;
                        this.pagination.total = res.data.data.total;
                    }
                    //查询失败
                    else {
                        console.log(res.data);
                        this.$message.error(res.data.message + '【' + res.data.data + '】')
                    }
                })
            },
            // 重置表单
            resetForm() {
                this.formData = {};
                if (this.$refs["dataAddForm"]!==undefined){
                    this.$refs["dataAddForm"].resetFields();
                }
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm();
                this.dialogFormVisible = true;
                // this.menuList = this.dataList;
                //下拉框显示方法
                axios.get("/menu/findParent.do").then((res)=>{
                    if (res.data.flag){
                        this.menuList = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                // alert(row.id);
                axios.get("/menu/findParent.do").then((res)=>{
                    if (res.data.flag){
                        this.menuList = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
                axios.post("/menu/findById/" + row.id + ".do").then((res) => {
                    if (res.data.flag) {
                        this.dialogFormVisible4Edit = true;
                        this.formData = res.data.data;

                    } else {
                        this.$message.error("获取数据失败，请刷新当前页面")
                    }
                })
            },

            //切换页码
            handleCurrentChange(currentPage) {
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                this.$confirm('确认删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get("/menu/deleteById/" + row.id + ".do").then((res) => {
                        if (res.data.flag) {
                            this.$message.success(res.data.message);
                        } else {
                            this.$message.error(res.data.message + '【' + res.data.data + '】');
                        }
                        this.findPage();
                    }).catch((r)=>{
                        // this.$message.error("1111111111111");
                        showMessage(r);

                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    })
</script>
</html>